package com.eyedsion.his.web.service.sycndata;

import com.eyedsion.his.web.dao.dec.syncdata.DecSyncLogRepository;
import com.eyedsion.his.web.entity.common.DecSyncLog;
import com.eyedsion.his.web.service.JpaBaseService;
import com.eyedsion.his.web.service.basedata.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 彭长云
 * @Description:同步基础数据
 * @date 2018/12/10 9:44
 */
@Service
public class BaseDataSercie {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IhBedService ihBedService;
    @Autowired
    private PubAccountLv1Service pubAccountLv1Service;
    @Autowired
    private PubAccountLv2Service pubAccountLv2Service;
    @Autowired
    private PubCostItemService pubCostItemService;
    @Autowired
    private PubCostPrimaryCategoryService pubCostPrimaryCategoryService;
    @Autowired
    private PubCostSecondCategoryService pubCostSecondCategoryService;
    @Autowired
    private PubDrugClassService pubDrugClassService;
    @Autowired
    private DecSyncLogRepository decSyncLogRepository;
    @Autowired
    private CpBscVariatReasonService cpBscVariatReasonService;
    @Autowired
    private CpClcpwService cpClcpwService;
    @Autowired
    private PubCombCostService pubCombCostService;
    @Autowired
    private PubCombCostDetailService pubCombCostDetailService;
    @Autowired
    private CommonService commonService;
    private final String lockName = "baseData";
    /**
     *
     *@author： 彭长云
     * @Description:基础数据同步，全量更新
     * @date： 2018/12/10 9:48
     */
    public DecSyncLog baseDataDs(){
        if(commonService.hasLock(lockName)){
            logger.info("基础数据正在同步，跳过此次同步");
            return new DecSyncLog("基础数据正在同步，跳过此次同步");
        }
        try {
            logger.info("开始同步基础数据。。。");
            //设置默认状态
            JpaBaseService.setSyncMsgMap("",DecSyncLog.SYNC_STATUS_SUCCESS);
            long startTime = System.currentTimeMillis();

            ihBedService.fullUpdate();
            pubAccountLv1Service.fullUpdate();
            pubAccountLv2Service.fullUpdate();
            pubCostItemService.fullUpdate();
            pubCostPrimaryCategoryService.fullUpdate();
            pubCostSecondCategoryService.fullUpdate();
            pubDrugClassService.fullUpdate();
            cpBscVariatReasonService.fullUpdate();
            cpClcpwService.fullUpdate();
            pubCombCostService.fullUpdate();
            pubCombCostDetailService.fullUpdate();

            long mi = (System.currentTimeMillis() - startTime) / 1000;
            logger.info("基础数据同步完成,共耗时:" + mi +"s");
        } catch (Exception e) {
           logger.error("基础数据同步出错",e);
        }finally {
            commonService.removeLock(lockName);
            //记录结果到表
            String syncStatus = JpaBaseService.getSyncMsgMap().get(getClass().getName()).getStatus();
            String result = DecSyncLog.SYNC_STATUS_ERROR.equals(syncStatus) ? "同步出现异常" : "同步成功";
            DecSyncLog decsynclog = decSyncLogRepository.save(
                    new DecSyncLog(DecSyncLog.TYPE_BASEDATA,syncStatus,DecSyncLog.SYNC_TYPE_QL,"基础数据同步",result
                    ));
            JpaBaseService.getSyncMsgMap().remove(getClass().getName());
            return decsynclog;
        }
    }

    public void baseDataDsByPage(){
        ihBedService.fullUpdate(true);
        //pubIllnessService.fullUpdate(true);
    }
}
